<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\Admin;
use app\admin\model\News;
use app\admin\model\Setting;
use app\admin\model\Filmtype;
use app\admin\model\Film;
use think\Request;
use think\File;
class  Classroom extends Base
{
     public $qr_src_path = "public/static/admin/uploads/Film_qrcode/";
    /*
     * 视频列表
     */
     public function lists(Request $request){
         $user = $this->user_info();
         $video_type_list = Filmtype::order("sort","asc")->select();
         $video_type_id =  !empty($request->route("videotype")) ? $request->route("videotype") : $video_type_list[0]['id'];
         $data=[
           "videotype"=>$video_type_list,
           "setting" => $this->getAllSetting(),
           "videtype_id"=> $video_type_id ? $video_type_id : $video_type_list[0]["id"],
         ];
         return view("list",$data);
     }
     /*
      * 查询视频
      */
    public function videolist(Request $request){
       $res =  Film::where(["videotype"=>$request->post("typeid")])->order("sort","asc")->select();
       return json(collection($res)->toArray());
    }
    /*
     * 修改视频的显示状态
     */
    public function VideoStatus(Request $request){
        $user = $this->user_info();
        if(Film::update($request->post())){
            $this->success("成功");
        }else{
            $this->error("失败");
        }
    }
    /*
     * 删除视频
     */
     public function video_del(Request $request){
       $data = $request->post();
         if(Film::destroy($data['id'])){
              $this->success("删除成功","Classroom/lists?videotype={$data['videotype']}");
         }else{
              $this->error("删除失败");
         }
     }
     /*
      * 上传视频封面
      */
      public function upload_cover(Request $request){
        //接收上传的文件
        $file = $this->request->file('file');
        if(!empty($file)){
          //图片存的路径
          $imgUrl= ROOT_PATH . 'public' . DS .'static'. DS .'admin'. DS .'uploads'. DS .'Film_cover';
          // 移动到框架应用根目录/public/uploads/ 目录下
          $info = $file->validate(['size'=>1048576*1024,'ext'=>'jpg,png,gif'])->rule('uniqid')->move($imgUrl);
          $error = $file->getError();
          //验证文件后缀后大小
          if(!empty($error)){
            dump($error);exit;
          }
          if($info){
            // 成功上传后 获取上传信息

            //获取图片的名字
            $imgName = $info->getFilename();
            //获取图片的路径
            $photo= $imgName;

          }else{
            // 上传失败获取错误信息
            $file->getError();
          }
        }else{
          $photo = '';
        }
        if($photo !== ''){
          return ['code'=>1,'msg'=>'成功','photo'=>$photo];
        }else{
          return ['code'=>404,'msg'=>'失败'];
        }
      }
      /*
       *上传视频
       */
       public function updata_info(){
         ini_set("max_execution_time","3600");
         ini_set("memory_limit","200M");
         ini_set("post_max_size","200M");
         echo ini_set("upload_max_filesize","200M");

         echo 'max_execution_time = ' . ini_get('max_execution_time') . "";
         echo 'memory_limit = ' . ini_get('memory_limit') . "";
         echo 'post_max_size = ' . ini_get('post_max_size') . "";
         echo 'upload_max_filesize = ' . ini_get('upload_max_filesize') . "";
       }
      public function upload_video(Request $request){
          //接收上传的文件
          $file = $this->request->file('file');
          if(!empty($file)){
            //图片存的路径
            $imgUrl= ROOT_PATH . 'public' . DS .'static'. DS .'admin'. DS .'uploads'. DS .'film';
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->validate(['size'=>1024*1024*1024,'ext'=>'mp4,rm,rmvb,mpeg1-4,mov,mtv,dat,wmv,avi,3gp,amv,dmv,flv'])->rule('uniqid')->move($imgUrl);
            $error = $file->getError();
            //验证文件后缀后大小
            if(!empty($error)){
              dump($error);exit;
            }
            if($info){
              //获取图片的名字
              $imgName = $info->getFilename();
              //获取图片的路径
              $photo= $imgName;
            }else{
              // 上传失败获取错误信息
              $file->getError();
            }
          }else{
            $photo = '';
          }
          if($photo !== ''){
            return ['code'=>1,'msg'=>'成功','video_url'=>$photo];
          }else{
            return ['code'=>0,'msg'=>'失败'];
          }
        }
      /*
       * 添加视频
       */
      public function addVideo(Request $request){
        $data = $request->post();
        $data["create_time"] = time();
        $data["update_time"] = time();
        $res = Film::create($data);
        if($res){
          Film::update(["id"=>$res['id'],"qrcode"=>$this->Qrcode(["video_id"=>$res['id']])]);
          $this->success("添加成功","Classroom/lists?videotype={$data['videotype']}");
        }else{
          $this->error("添加视频失败");
        }
      }

      /*
       * 修改视频
       */
      public function update_video(Request $request){
        $data = $request->post();
        $data["update_time"] = time();
        if(Film::update($data)){
          $this->success("修改成功","Classroom/lists?videotype={$data['videotype']}");
        }else{
          $this->error("修改视频失败");
        }
      }

    /*-----------------------------------------------------------------------------------------*/
    //视频分类列表
    public function type_list(){
        $user = $this->user_info();
        $resource = collection(Filmtype::order("sort asc")->select())->toArray();
        foreach($resource as $k => $v){
            $sum = Film::where(array("Videotype"=>$v['id']))->count();
            $resource[$k]['sum'] = $sum;
        }
        $data = array(
            "setting" => $this->getAllSetting(),
            "list"=>$resource,
        );
        return view("typelist",$data);
    }
    //添加
    public function type_addform(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
        );
        return view("type_addform",$data);
    }
    public function type_add(Request $request){
        //接收所有的请求数据，数组：多个数据的集合
        $data = $request->post();
        // 创建数组元素
        $data['create_time'] = time();
        $data['update_time'] = time();
        $res = Filmtype::insert($data);  //执行数据库命令
        //返回结果的判断
        if($res){
            $this->success("添加成功","Classroom/type_list");
        }else{
            $this->error("添加失败","Classroom/type_addform");
        }
    }

    //删除分类
    public function type_deletes(Request $request){
        $user = $this->user_info();
        $id = $request->post('id');
        //删除分类的时先去新闻标中查看该分类下是否有新闻，如果有则不允许删除
        $sum =  Film::where(["videotype"=>$id])->count();
        if($sum > 0){
            $this->error("该分类有视频，请将视频迁移之后或删除该分类下的所有视频后在删除分类！");
            exit();
        }
        //如果该分类下没有新闻则直接删除
        if(Filmtype::destroy($id)){
            $this->success("删除成功","Classroom/type_list");
        }else{
            $this->error("删除失败");
        }
    }
    //修改分类状态
    public function setTypeStatus(Request $request){
        $user = $this->user_info();
        if(Filmtype::update($request->post())){
            $this->success("成功","Classroom/type_list");
        }else{
            $this->error("失败");
        }
    }
    //修改分类
    public function type_update(Request $request){
        $user = $this->user_info();
        $id = $request->get("id");
        $data = array(
            "setting" => $this->getAllSetting(),
            "data" => Filmtype::where(array("id"=>$id))->find(),
        );
        return view("type_update",$data);
    }
    public function type_toUpdate(Request $request){
        $user = $this->user_info();
        $data = $request->post();
        $data['update_time'] = time();
        if(Filmtype::update($data)){
            $this->success("修改成功","Classroom/type_list");
        }else{
            $this->error("修改失败");
        }
    }

  //生成二维码
  public function Qrcode($arr){
    $setting = $this->getAllSetting();
    //组织$query
    $query = "";
    foreach($arr as $key=>$val){
      $query .= $key."/".$val."/";
    }
    $query = substr($query,0,-1);
    $str = $setting['video_qrcode_url']."/".$query;
    return  $this->qr_code_dir($str,"film_qrcode");
  }
  //重新生成登录二维码
  public function re_qrcode(Request $request){
    $id = $request->post("id");
    //删除之前生成的二维码
    $res = Film::get($id);
    if($res['qrcode']) {
      delete_file($this->qr_src_path . $res['qrcode']); //删除数据的时候连同生成的图片一起删除
    }
    $qr_code = $this->Qrcode(['id'=>$id]); //创建新的二维码
    $ress = Film::update(['id'=>$id,"qrcode"=>$qr_code]);
    if ($ress) {
      $this->success("更新成功","Classroom/lists?videotype={$request->post['videotype']}");
    } else {
      $this->error("更新失败");
    }
  }

}
